import { Injectable }     from '@angular/core';
import { Http,ResponseContentType,RequestMethod,URLSearchParams,Headers,RequestOptions,Request,RequestOptionsArgs} from '@angular/http';
import { API,HOST,CONFIG } from '../config/app.config';
import {Observable} from "rxjs/Observable";
import {CookieService} from 'ng2-cookies';
export interface MyHttpAPIOptions{
  url:string;
  method:string|RequestMethod;
}
export interface MyRequestOptionsArgs{

}
export interface MyHttpParameters{
  api:MyHttpAPIOptions;
  body?:any,
  query?:any,
  params?:any,
  //options?:RequestOptionsArgs
  options?:any
}
@Injectable()
export class MyHttp{
    api=API;
   private cookie:CookieService=new CookieService();
    constructor(private http:Http){
    }

    private initDefault(parameters:MyHttpParameters){
      if(parameters.options===undefined){
        parameters.options={
          withCredentials:true,
          headers:{
            authorization:this.cookie.get('sid')
          }
        };
      }else{
        if(parameters.options.withCredentials===undefined){
          parameters.options.authorization=true;
        }
        if(parameters.options.headers===undefined){
          parameters.options.headers={
            authorization:this.cookie.get('sid')
          }
        }else if(parameters.options.headers.authorization===undefined){
          parameters.options.headers.authorization=this.cookie.get('sid');
        }
      }
    }
    /**
     * 构建url
     * @param parameters
     * @returns {string}
     */
    private createUrl(parameters:MyHttpParameters):string{
        let url:string=parameters.api.url;
        let params=parameters.params;
        let query=parameters.query;
        if(params&&typeof params==='object'){//替换url中形如 /:id 的参数
            for(let o in params){
                let reg=new RegExp("\/\\s*:\\s*"+o+"[^/?]*");
                url=url.replace(reg,'/'+params[o]);
            }
        }
        if(query&&typeof query==='object'){//替换url中形如  ?name=1&value=2  的数据
            for(let o in query){
                let reg=new RegExp("[^?&/]*"+o+"\\s*=?[^?&/]*");
                url=url.replace(reg,o+'='+query[o])
            }
        }
        url=CONFIG.env?HOST.prod+url:HOST.dev+url;
        return url;
    }

    /**
     * request
     * @param parameters
     * @returns {Promise<T>}
     */
    request(parameters:MyHttpParameters):Observable<any>{
        let url=this.createUrl(parameters);
        this.initDefault(parameters);
        return this.http.request(url,parameters.options);
    }

    /**
     * get
     * @param parameters
     * @returns {Promise<T>}
     */
    get(parameters:MyHttpParameters):Observable<any>{
        console.log(parameters);
        let url=this.createUrl(parameters);
      this.initDefault(parameters);
        return this.http.get(url,parameters.options);
    }

    /**
     * post
     * @param parameters
     * @returns {Promise<T>}
     */
    post(parameters:{
      api:MyHttpAPIOptions,
      body:any,
      query?:any,
      params?:any,
      options?:any
    }):Observable<any>{
        let url=this.createUrl(parameters);
      this.initDefault(parameters);
       let body=parameters.body||parameters.options.body;
        return this.http.post(url,body,parameters.options);
    }

    /**
     * put
     * @param parameters
     * @returns {Promise<T>}
     */
    put(parameters:{
      api:MyHttpAPIOptions,
      body:any,
      query?:any,
      params?:any,
      options?:any
    }):Observable<any>{
        let url=this.createUrl(parameters);
      this.initDefault(parameters);
        let body=parameters.body||parameters.options.body;
        return this.http.put(url,body,parameters.options);
    }

    /**
     * delete
     * @param parameters
     * @returns {Promise<T>}
     */
    delete(parameters:MyHttpParameters):Observable<any>{
        let url=this.createUrl(parameters);
       this.initDefault(parameters);
        return this.http.delete(url,parameters.options);
    }

    /**
     * patch
     * @param parameters
     * @returns {Promise<T>}
     */
    patch(parameters:{
      api:MyHttpAPIOptions,
      body:any,
      query?:any,
      params?:any,
      options?:any
    }):Observable<any>{
        let url=this.createUrl(parameters);
        this.initDefault(parameters);
        let body=parameters.body||parameters.options.body;
        return this.http.patch(url,body,parameters.options);
    }

    /**
     * head
     * @param parameters
     * @returns {Promise<T>}
     */
    head(parameters:MyHttpParameters):Observable<any>{
        console.log(parameters);
        let url=this.createUrl(parameters);
        this.initDefault(parameters);
        return this.http.head(url,parameters.options);
    }

    /**
     * options
     * @param parameters
     * @returns {Promise<T>}
     */
    options(parameters:MyHttpParameters):Observable<any>{
        console.log(parameters);
        let url=this.createUrl(parameters);
      this.initDefault(parameters);
        return this.http.options(url,parameters.options);
    }
}
